//
//  NLVisitorView.swift
//  sinaWeibo7110
//
//  Created by 安静清晨 on 2017/1/5.
//  Copyright © 2017年 walk-in-minds. All rights reserved.
//

import UIKit
import SnapKit

//1. 声明协议和协议方法
@objc protocol NLVisitorViewDelegate : NSObjectProtocol {
    
   @objc optional func willLogin()
    
   @objc optional func willRegister()
    
    
}



class NLVisitorView: UIView {

    //2. 声明代理属性
    weak var delegate : NLVisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func updateUI(IconName:String, lbl_text:String, isHome:Bool){
        
        self.circleChan.image = UIImage(named: IconName)
        self.wordsChan.text = lbl_text
        
        if  isHome {
            
           startAnimation()
            
        }else{
            
            self.houseChan.isHidden = true
            self.maskChan.isHidden = true
        }
        
        
    }
    
    
  
    
    private func startAnimation (){
        
        //转动动画
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        animator.repeatCount = MAXFLOAT
        animator.toValue = M_PI * 2
        animator.duration = 20
        animator.isRemovedOnCompletion = false
        circleChan.layer.add(animator, forKey: nil)
        
    }

    
    
    func willLogin(){
        
        
        delegate?.willLogin?()
        
        
    }
    
    
    func willRegister(){
        
        delegate?.willRegister?()
        
    }
    
    
    private func setupUI(){
        
    

        addSubview(circleChan)
        addSubview(maskChan)
        addSubview(houseChan)
        addSubview(wordsChan)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        loginBtn.addTarget(self, action: #selector(willLogin), for: .touchUpInside)
        
        registerBtn.addTarget(self, action: #selector(willRegister), for: .touchUpInside)
        
 
        //約束

        houseChan.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
            
        }
        
        circleChan.snp.makeConstraints { (make) in
            
            make.center.equalTo(houseChan)
        }
        
        
        maskChan.snp.makeConstraints { (make) in
        
            make.center.equalTo(houseChan)
        }
        
        wordsChan.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(houseChan)
            make.top.equalTo(circleChan.snp.bottom)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            
            make.leading.equalTo(wordsChan)
            make.top.equalTo(wordsChan.snp.bottom).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(30)
            
        }
        
        registerBtn.snp.makeConstraints { (make) in
            
            make.trailing.equalTo(wordsChan)
            make.top.equalTo(loginBtn)
            make.width.height.equalTo(loginBtn)
        }
        
        
        
    }
    
    
    private lazy var houseChan: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    private lazy var circleChan:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    private lazy var maskChan:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var wordsChan:UILabel = {
        
        let wordsLbl = UILabel(title: "注册了关注一些人回来看看有什么毛线惊喜", BGImageName: "", size: 14, color: UIColor.darkGray)
        
        //换行
        //1.
        wordsLbl.numberOfLines = 0
        
        //2
        wordsLbl.preferredMaxLayoutWidth = 220
        
        return wordsLbl
        //
    }()
    
  
    
    lazy var loginBtn:UIButton = {
        
        let btn = UIButton(title: "登录", BGImageName: "common_button_white", size: 14, color: UIColor.orange)
        
        return btn
    }()
    
    lazy var registerBtn:UIButton = {
        
        let btn = UIButton(title: "注册", BGImageName: "common_button_white", size: 14, color: UIColor.orange)
        return btn
    }()
}
